<?php
/** Zend_Controller_Action */ 
require_once ROOT_PATH . '/library/Soooner/Controller/Action.php';
class AdminController extends Soooner_Controller_Action
{
	private $_userid;
	private $_modelAdmin;
    public function init()
    {
        /* Initialize action controller here */
    	$this->initDzTemplate();
    	$this->_modelAdmin = new Model_Tables_Admin();
    }
    /**
    * 获取管理员列表
    * @return unknown_type
    */
    public function indexAction()
    {
    	$options = $GLOBALS['application']->getOptions();
    	$numPerPage = $options["pagecount"];//每页显示的条数
        $ItemCount =  $this->_modelAdmin->getCount();
        $PageCount = ceil($ItemCount/$numPerPage);
        $PageCount = $PageCount ? $PageCount : 1;
       
        $rolemodel = new Model_Tables_Role();
    	$roles= $rolemodel->getRoleList();  
		include $this->_dzTemplate->getFile( 'admin.manage.html' );
    }
    /**
    * 获取管理员列表
    * @return unknown_type
    */
    public function indexajaxAction()
    {
        $options = $GLOBALS['application']->getOptions();
    	$numPerPage = $options["pagecount"];//每页显示的条数
        $page = intval($this->_getParam('page'));//默认页码
        $page = empty($page)?'1':$page;
   		$s = $this->_getParam('s'); 
        $ItemCount =  $this->_modelAdmin->getCount($s);
        $PageCount = ceil($ItemCount/$numPerPage);
        if($page>$PageCount)$page=$PageCount;
        $infos = $this->_modelAdmin->getList( $page, $numPerPage, $s );
        include $this->_dzTemplate->getFile( 'ajax/admin.manage.ajax.html' );
    }
    /**
    * 添加管理员
    * @return unknown_type
    */
    public function addAction()
    {
    	$template = new Soooner_View_Template(); //使用的模板引擎
    	$request = $this->getRequest();     
    	$rolemodel = new Model_Tables_Role();
    	$roles= $rolemodel->getRoleList();  
    	$spinfomodel = new Model_Tables_Spinfo();
    	$splist = $spinfomodel->getSpList();
    	//print_r($splist);exit;
    	
        if ($request->isPost())
        {
            $params =$request->getPost();
            $this->_modelAdmin->add($params);                
            $this->_redirect("/admin/index/");
        } 
        include $this->_dzTemplate->getFile( 'admin.add.html' );
    }
    /**
    * 修改管理员
    * @return unknown_type
    */
    public function modifyAction()
    {
    	$template = new Soooner_View_Template();
    	$userid = $this->_getParam('userid');
    	$info = $this->_modelAdmin->getInfo($userid); 
        $request = $this->getRequest();

        $rolemodel = new Model_Tables_Role();
    	$roles= $rolemodel->getRoleList();  
    	$adminrolemodel = new Model_Tables_AdminRole();
    	$valueadminrole = $adminrolemodel->getAdminRole("$userid");   
    	$spinfomodel = new Model_Tables_Spinfo();
    	$splist = $spinfomodel->getSpList(); 	
    	
        if ($request->isPost())
        {
        	$request =$request->getPost();        	
            if (true)//服务端检查输入参数
            {
            	$this->_modelAdmin->modify($request,$userid);
                $this->_redirect("/admin/");
            }
        } 
        include $this->_dzTemplate->getFile( 'admin.edit.html' );
    }
    /**
    * 无刷新修改管理员
    * @return unknown_type
    */    
    public function modifyajaxAction()
    {
        $request = $this->getRequest();
        $params = $request->getPost();
        $arr = split("\|", $params['id']);
        $field = $arr[0];
        $userid = $arr[1];
        $value = stripslashes($params['value']);
        $this->_modelAdmin->modifyAjax($userid,$field,$value);
        echo $value;
        exit;
    }
    public function modifyajaxreversAction()
    {
    	$field = $this->_getParam('field');
    	$pri = $this->_getParam('pri');
        $this->_modelAdmin->modifyAjaxRevers($field,$pri);
        exit;
    }
    /**
    * 删除管理员
    */
    public function deleteAction()
    {
        $userid = $this->_getParam('userid');   
        $this->_modelAdmin->del($userid); 
        $this->_redirect("/admin/");
    }
    
 	/**
    * 修改密码
    * @return unknown_type
    */
    public function passwordAction()
    {
    	$userinfo = $this->_userinfo;
    	$request = $this->getRequest();
        if ($request->isPost())
        {
        	$request =$request->getPost();        	
            if ($request)//服务端检查输入参数
            {
            	$this->_modelAdmin->updatepassword($request);
                echo '<script>alert("修改成功。");</script>';
            }
        }        
        include $this->_dzTemplate->getFile( 'admin.password.html' );
    }
    
    public function checkpasswordAction()
    {
    	$password = $this->_getParam('password');
    	$userid = $this->_userinfo['userid'];//$this->_getParam('userid');
//    	echo $password.'<br>'.$userid;exit;
    	$ok = $this->_modelAdmin->checkpassword($userid,$password);
    	if($ok)
    	{
    		echo '1';exit;
    	}
    	else
    	{
    		echo '0';exit;
    	}
    }
}

